//
//  FscrollView.m
//  中国太平保险app
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 rimi. All rights reserved.
//
//#import <UIButton+WebCache.h>
#import "FscrollView.h"

@interface FscrollView ()<UIScrollViewDelegate>
@property(nonatomic,strong) UIScrollView *scroll;
@property(nonatomic,strong) NSTimer *timer;

@property(nonatomic,strong) NSMutableArray *imageViews;

@property(nonatomic,assign) NSInteger currentIndex;
@property(nonatomic,assign) BOOL isShowPageImage;

@end


@implementation FscrollView

-(instancetype )initWithFrame:(CGRect)rect imageNames:(NSArray *)imageNames{
    
    if (self=[super initWithFrame:rect]) {
        self.currentIndex=0;
        self.imageNames=imageNames;
        [self initWithUserinterFace:rect];
    }
    return self;
}

-(instancetype )initWithFrame:(CGRect)rect imageNames:(NSArray *)imageNames showPageImage:(BOOL)showPageImg{
    
    if (self=[super initWithFrame:rect]) {
        self.currentIndex=0;
        self.imageNames=imageNames;
        self.isShowPageImage=showPageImg;
        [self initWithUserinterFace:rect];
    }
    return self;
}
-(void)dealloc{

    [self stopTimer];
}
-(void)setShowPageControl:(BOOL)showPageControl{

    _showPageControl=showPageControl;
    self.pagecontrol.hidden=!showPageControl;
}
-(void)setImageNames:(NSArray *)imageNames{

    _imageNames=imageNames;
    
}

-(void)initWithUserinterFace:(CGRect)rect{
    [self addSubview:self.scroll];
    if (self.imageNames.count>1) {
        [self startTimer];
    }
    self.backgroundColor=[UIColor blackColor];
    [self addSubview:self.pagecontrol];
//    self.pagecontrol.centerX=self.centerX;
//    self.pagecontrol.top=self.height-15;
    for (int i=0; i<3; i++) {
        
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setHighlighted:NO];
        [btn addTarget:self action:@selector(handleImg:) forControlEvents:UIControlEventTouchUpInside];
//        btn.contentMode=UIViewContentModeScaleToFill;
        [btn addTarget:self action:@selector(touchDone) forControlEvents:UIControlEventTouchDown];
        btn.backgroundColor=[UIColor clearColor];
        btn.frame=CGRectMake(rect.size.width*i+5, 0, rect.size.width-10, rect.size.height);
        btn.highlighted=NO;
        btn.backgroundColor=[UIColor redColor];
        [self.scroll addSubview:btn];
        [self.imageViews addObject:btn];
    }
    [self DynamicSetScrollImages:self.currentIndex];
    
}

-(void)handleImg:(UIButton*)sender{
    if (self.imageNames.count==0) {
        return;
    }
//    YSLog(@"%@",self.imageNames[(self.currentIndex)%self.imageNames.count]);

    if (self.block) {
        self.block(self.currentIndex);
    }
    if (self.imageNames.count>1) {
        [self startTimer];
    }
    
}
-(void)touchDone{
    if (self.imageNames.count>1) {
         [self stopTimer];
    }
   
}

-(UIScrollView*)scroll{

    if (!_scroll) {
        _scroll=[[UIScrollView alloc] initWithFrame:self.bounds];
        _scroll.delegate=self;
        _scroll.pagingEnabled=YES;
        _scroll.showsHorizontalScrollIndicator=NO;
        _scroll.contentSize=CGSizeMake(3*self.frame.size.width, 0);
        _scroll.clipsToBounds=NO;
        
    }
    return _scroll;
}
-(UIPageControl*)pagecontrol{

    if (!_pagecontrol) {
        _pagecontrol= [[UIPageControl alloc] init];
        _pagecontrol.numberOfPages=self.imageNames.count;
        _pagecontrol.currentPage=_currentIndex;

        _pagecontrol.pageIndicatorTintColor=[UIColor whiteColor];
        _pagecontrol.currentPageIndicatorTintColor=[UIColor greenColor];
        _pagecontrol.frame=CGRectMake(0, 0, 414, 10);
//        _pagecontrol.hidden=!self.showPageControl;
        _pagecontrol.userInteractionEnabled=NO;
        
    }
    return _pagecontrol;
}

-(void)startTimer{

    if (!_timer) {
        _timer=[NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerHandle) userInfo:nil repeats:YES];
          [[NSRunLoop mainRunLoop] addTimer:_timer forMode: NSDefaultRunLoopMode];
    }
    _timer.fireDate=[NSDate dateWithTimeIntervalSinceNow:3];
    
    
}
-(void)timerHandle{
    [self.scroll setContentOffset:CGPointMake(2*self.frame.size.width, 0) animated:YES];
}
-(void)stopTimer{

    [_timer invalidate];
    _timer=nil;
}
-(NSMutableArray*)imageViews{

    if (!_imageViews) {
        _imageViews=[NSMutableArray array];
    }
    return _imageViews;
}
-(void)DynamicSetScrollImages:(NSInteger)index{
   
    self.pagecontrol.currentPage=index;
    if (self.imageNames.count==0) {
        return;
    }
    for (int i=0; i<3; i++) {
        // 获取正确图片名字下标
        NSInteger rightindex=(index+i+self.imageNames.count-1)%self.imageNames.count;
        NSDictionary *dic=self.imageNames[rightindex];
          UIButton *btn= self.imageViews[i];

//        NSString *imgurl=dic[@"image"];
//                                NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MainUrl,imgurl]];
                               
//            [btn sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:nil];
        
            btn.imageView.contentMode=UIViewContentModeScaleToFill;
//        for (UIView *view in btn.subviews) {
//            if ([view isKindOfClass:[UILabel class]]) {
//                UILabel *lab=(UILabel*)view;
//                lab.text=[NSString stringWithFormat:@"  %@",dic[@"title"]];
//            }
//        }
       
    }
   
    [self.scroll setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
 
   

    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if(self.imageNames.count==0){
    
        return;
    }
    if (scrollView.contentOffset.x<=0) {
        self.currentIndex=(--self.currentIndex+self.imageNames.count)%self.imageNames.count;
    }
    else if (scrollView.contentOffset.x>=2*self.frame.size.width){
    
        self.currentIndex=(++self.currentIndex)%self.imageNames.count;
    }
    
    
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    
    [self scrollViewDidEndDecelerating:scrollView];
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
   // 动态加载图片
    [self DynamicSetScrollImages:self.currentIndex];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (self.imageNames.count>1) {
         [self stopTimer];//
    }
   
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (self.imageNames.count>1) {
           [self startTimer];
       }
    
}


@end
